<template>
  <div>
    <el-button type="primary" @click="handleSyncMenus">同步菜单</el-button>
  </div>
</template>

<script>

import RouterUtil from "@/framework/infra/router/RouterUtil";
import {permissionApi} from "@/feature/mgr/infra/api/permissionApi";

export default {
  methods: {
    handleSyncMenus() {
      this.$confirm('确定同步吗?', '提示').then(() => {
        let routes = RouterUtil.getRoutes()
        console.log(routes)
        routes = this.getNames(routes)
        // console.log(routes)
        return permissionApi.syncPermissionCodes(routes)
      }).then(() => {
        this.$message.success('同步成功')
      })
    },
    getNames(routes) {
      return routes.reduce((r, a) => {
        a.children.forEach(c => {
          c.buttons.forEach(b => {
            if (!b.name) {
              return
            }
            if (!r.some(el => el.code === b.name)) {
              r.push({
                code: b.name,
                name: c.label + ':' + b.label,
                module_name: a.meta.title,
                business_type: 1
              })
            }
          })
        })
        return r
      }, [])
    }
  }
}
</script>

<style scoped lang="scss">

</style>
